﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;

namespace LAura
{
    /// <summary>
    /// Interaction logic for Options.xaml
    /// </summary>
    public partial class Options : Window
    {
        #region Member
        #endregion



        #region Properties

        public event EventHandler OptionsChanged;

        #endregion



        #region Constructor

        public Options()
        {
            InitializeComponent();
        }

        #endregion



        #region Methods

        private void WindowSourceInitialized(object sender, EventArgs e)
        {
            var handle = new WindowInteropHelper(this).Handle;

            var margins = new WinAPI.Margins { cyTopHeight = 34 };

            var hwndSrc = HwndSource.FromHwnd(handle);
            if (hwndSrc != null && hwndSrc.CompositionTarget != null)
            {
                hwndSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
            }

            Dwm.ExtendGlassFrame(handle, ref margins);

            var fileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
            BuildTag.Text = Assembly.GetExecutingAssembly().GetName().Version + ".beta." + fileInfo.LastWriteTimeUtc.ToString("yyMMdd-HHmm");

            RunFromStartupCheckBox.IsChecked = App.Settings.RunFromStartup;

            ApplyButton.IsEnabled = false;
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            if (ApplyButton.IsEnabled)
            {
                ApplySettings();
            }
            Close();
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void ApplyButtonClick(object sender, RoutedEventArgs e)
        {
            ApplySettings();
        }

        private void ApplySettings()
        {
            var runFromStartup = RunFromStartupCheckBox.IsChecked;

            if (runFromStartup != null)
            {
                App.Settings.RunFromStartup = (bool) runFromStartup;
            }

            App.Settings.WriteRegistry();

            if (OptionsChanged != null)
            {
                OptionsChanged(null, EventArgs.Empty);
            }

            ApplyButton.IsEnabled = false;
        }

        private void CheckboxClick(object sender, RoutedEventArgs e)
        {
            ApplyButton.IsEnabled = true;
        }

        /// <summary>
        /// not in use
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ApplyButton.IsEnabled = true;
        }

        /// <summary>
        /// not in use
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GoToColorSettingsLinkMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Process.Start("control.exe", "/name Microsoft.Personalization /page pageColorization");
        }

        private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        #endregion
    }
}
